package cycling.taxi.com.person;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cycling.taxi.com.IConstants;
import cycling.taxi.com.R;
import cycling.taxi.com.request.Config;
import cycling.taxi.com.request.RM;
import cycling.taxi.com.request.RequestBean;
import cycling.taxi.com.request.RequestCallback;
import cycling.taxi.com.request.RequestParams;
import cycling.taxi.com.request.RequestUtil;
import cycling.taxi.com.utils.ToastUtil;
import cycling.taxi.com.utils.Urls;

/**
 * Created by Administrator on 2017/5/15.
 */
public class UpdataPawAty extends AppCompatActivity implements View.OnClickListener,RequestCallback{
    @InjectView(R.id.updata_phone)
    EditText mUpdataPhone;
    @InjectView(R.id.updata_former_paw)
    EditText mUpdataFormerPaw;
    @InjectView(R.id.updata_new_paw)
    EditText mUpdataNewPaw;
    @InjectView(R.id.updata_enter)
    Button mUpdataEnter;
    @InjectView(R.id.back)
    ImageView mBack;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aty_updata_pwd);
        ExitApplication.getInstance().addActivity(this);
        ButterKnife.inject(this);

        setListener();
    }

    private void setListener() {
        mUpdataEnter.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.updata_enter:
                updataPaw();
                break;
            case R.id.back:
                finish();
                break;
        }
    }

    private void updataPaw() {
        //拿到UI输入过来的数据
        String phone = mUpdataPhone.getText().toString().trim();
        String formerPaw = mUpdataFormerPaw.getText().toString().trim();
        String newPaw = mUpdataNewPaw.getText().toString().trim();

        //发起网络请求
        RequestParams params = new RequestParams.Builder()
                .putParam("mobile", phone)
                .putParam("oldPassword", formerPaw)
                .putParam("newPassword", newPaw)
                .build();

        //开始请求发送数据到服务器服务器
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        String url = Urls.updataPaw();//回去api路径

        RequestUtil.requestData(requestQueue,
                IConstants.UPDATAPAW,
                url,
                RM.GET,
                RequestBean.class,
                params,
                this);

    }

    @Override
    public void requestSuccess(RequestBean bean, Config config) {
        ToastUtil.getInstance().toast("修改密码成功");

        finish();
    }

    @Override
    public void requestFail(RequestBean bean, Config config) {
        ToastUtil.getInstance().toast("服务器异常,网络连接失败");
    }

    @Override
    public void requestTokenInvalid(String requestTag, Config config) {

    }
}
